package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Tuser;
import com.example.exception.CustomException;
import com.example.mapper.TuserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class TuserService extends ServiceImpl<TuserMapper, Tuser> {

    @Resource
    private TuserMapper tuserMapper;

    public Tuser login(Tuser tuser) {
            LambdaQueryWrapper<Tuser> queryWrapper = Wrappers.<Tuser>lambdaQuery().eq(Tuser::getUsername, tuser.getUsername()).eq(Tuser::getPassword,tuser.getPassword());//查询账号和密码
            Tuser one = getOne(queryWrapper);
            if (one == null) {
                throw new CustomException("-1", "账号或密码错误");
            }
            return one;
    }
    public Tuser register(Tuser tuser) {
        Tuser one = getOne((Wrappers.<Tuser>lambdaQuery().eq(Tuser::getUsername, tuser.getUsername())));
        if (one != null) {
            throw new CustomException("-1", "用户已注册");
        }
        if (tuser.getPassword() == null) {
            tuser.setPassword("123456");
        }
        save(tuser);
        return getOne((Wrappers.<Tuser>lambdaQuery().eq(Tuser::getUsername, tuser.getUsername())));
    }

    public Tuser getbyUsername(String username) {
        Tuser one = getOne((Wrappers.<Tuser>lambdaQuery().eq(Tuser::getUsername, username)));
        return one;
    }

}
